```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>锁机制详解 - 多线程编程核心技术</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
            background-color: #f9fafb;
        }
        .serif {
            font-family: 'Noto Serif SC', serif;
        }
        .hero {
            background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
        }
        .card {
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .code-block {
            background-color: #1e293b;
            border-radius: 0.5rem;
        }
        .drop-cap:first-letter {
            float: left;
            font-size: 3.5rem;
            line-height: 1;
            margin: 0 0.5rem 0 0;
            color: #3b82f6;
            font-weight: bold;
        }
        .hover-grow {
            transition: transform 0.2s ease;
        }
        .hover-grow:hover {
            transform: scale(1.02);
        }
        .diagram-container {
            background-color: white;
            border-radius: 0.5rem;
            padding: 1.5rem;
            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }
    </style>
</head>
<body class="antialiased">
    <!-- Hero Section -->
    <section class="hero text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="serif text-4xl md:text-5xl font-bold mb-4">深入理解Java锁机制</h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8">多线程编程中的核心并发控制技术</p>
            <div class="flex justify-center space-x-4">
                <a href="#intro" class="bg-white text-blue-600 hover:bg-gray-100 px-6 py-3 rounded-lg font-medium transition duration-300">开始阅读</a>
                <a href="#types" class="border-2 border-white hover:bg-white hover:text-blue-600 px-6 py-3 rounded-lg font-medium transition duration-300">锁的类型</a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="max-w-5xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Introduction Section -->
        <section id="intro" class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-blue-500 mr-4"></div>
                <h2 class="serif text-3xl font-bold">锁机制基础</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-lock text-blue-500"></i>
                        </div>
                        <h3 class="text-xl font-semibold">什么是锁</h3>
                    </div>
                    <p class="text-gray-700 drop-cap">通过锁机制，能够保证在多线程环境中，在某一个时间点上，只能有一个线程进入临界区代码，从而保证临界区中操作数据的一致性。所谓的锁，可以理解为内存中的一个整型数，拥有两种状态：空闲状态和上锁状态。加锁时，判断锁是否空闲，如果空闲，修改为上锁状态，返回成功。如果已经上锁，则返回失败。解锁时，则把锁状态修改为空闲状态。</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-question-circle text-blue-500"></i>
                        </div>
                        <h3 class="text-xl font-semibold">为什么需要锁</h3>
                    </div>
                    <p class="text-gray-700">在多线程情况下完成操作时，由于并不是原子操作，所以在完成操作的过程中可能会被打断，造成数据的不一致。比如在电商系统中常见的"超卖"问题，就是一个典型的需要锁机制来解决的并发问题。</p>
                </div>
            </div>

            <div class="mb-12">
                <div class="flex items-center mb-4">
                    <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                        <i class="fas fa-code text-blue-500"></i>
                    </div>
                    <h3 class="text-xl font-semibold">代码示例</h3>
                </div>
                <div class="code-block p-4 rounded-lg overflow-x-auto">
                    <pre class="text-gray-300"><code>package com.qf.day30lock01hello.base;

import lombok.Data;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadDemo {
    public static void main(String[] args) throws Exception {
        User user = new User();
        for (int i = 0; i < 500; i++) {
            new Thread(() -> {
                int age = user.getAge().incrementAndGet();
                try {
                    Thread.sleep(10 * new Random().nextInt(3));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        Thread.sleep(5000);
        System.out.println(user.getAge().get());
    }
}

@Data
class User {
    private AtomicInteger age = new AtomicInteger(0);
}</code></pre>
                </div>
            </div>
        </section>

        <!-- Types of Locks Section -->
        <section id="types" class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-blue-500 mr-4"></div>
                <h2 class="serif text-3xl font-bold">锁的种类与实现</h2>
            </div>

            <!-- 乐观锁/悲观锁 -->
            <div class="mb-12">
                <div class="bg-white rounded-xl shadow-md p-6 mb-6 card">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-balance-scale text-blue-500"></i>
                        </div>
                        <h3 class="text-xl font-semibold">乐观锁 vs 悲观锁</h3>
                    </div>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="p-4 bg-blue-50 rounded-lg hover-grow">
                            <h4 class="font-semibold text-blue-700 mb-2 flex items-center">
                                <i class="fas fa-smile mr-2"></i> 乐观锁
                            </h4>
                            <p class="text-gray-700">乐观锁总是认为不存在并发问题，每次去取数据的时候，总认为不会有其他线程对数据进行修改，因此不会上锁，但是在更新的时候会判断一下在此期间别人有没有去更新这个数据。一般使用"数据版本机制"或"CAS操作"实现。</p>
                            <div class="mt-3">
                                <span class="inline-block bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded mr-2">CAS</span>
                                <span class="inline-block bg-blue-100 text-blue-800 text-xs px-2 py-1 rounded">版本号机制</span>
                            </div>
                        </div>
                        
                        <div class="p-4 bg-purple-50 rounded-lg hover-grow">
                            <h4 class="font-semibold text-purple-700 mb-2 flex items-center">
                                <i class="fas fa-frown mr-2"></i> 悲观锁
                            </h4>
                            <p class="text-gray-700">每次去拿数据的时候都认为别人会修改，所以每次在拿数据的时候都会上锁，这样别人想拿这个数据就会阻塞直到它拿到锁。Java中的synchronized关键字就是悲观锁的实现。悲观锁适合写操作非常多的场景。</p>
                            <div class="mt-3">
                                <span class="inline-block bg-purple-100 text-purple-800 text-xs px-2 py-1 rounded">synchronized</span>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <h4 class="font-semibold mb-3">数据版本机制示例</h4>
                    <div class="code-block p-4 rounded-lg overflow-x-auto">
                        <pre class="text-gray-300"><code>update table set xxx=#{xxx}, version=version+1 
where id=#{id} and version=#{version};</code></pre>
                    </div>
                </div>
            </div>

            <!-- 独享锁/共享锁 -->
            <div class="mb-12">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-user-lock text-blue-500"></i>
                        </div>
                        <h3 class="text-xl font-semibold">独享锁 vs 共享锁</h3>
                    </div>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="p-4 bg-yellow-50 rounded-lg hover-grow">
                            <h4 class="font-semibold text-yellow-700 mb-2 flex items-center">
                                <i class="fas fa-user-shield mr-2"></i> 独享锁
                            </h4>
                            <p class="text-gray-700">独享锁是指该锁一次只能被一个线程所持有。Java中的ReentrantLock和synchronized都是独享锁的实现。独享锁保证了在任何时候只有一个线程能访问共享资源。</p>
                            <div class="mt-3">
                                <span class="inline-block bg-yellow-100 text-yellow-800 text-xs px-2 py-1 rounded mr-2">ReentrantLock</span>
                                <span class="inline-block bg-yellow-100 text-yellow-800 text-xs px-2 py-1 rounded">synchronized</span>
                            </div>
                        </div>
                        
                        <div class="p-4 bg-green-50 rounded-lg hover-grow">
                            <h4 class="font-semibold text-green-700 mb-2 flex items-center">
                                <i class="fas fa-users mr-2"></i> 共享锁
                            </h4>
                            <p class="text-gray-700">共享锁是指该锁可被多个线程所持有。Java中ReadWriteLock的读锁就是共享锁的实现。共享锁适用于读多写少的场景，可以显著提高并发性能。</p>
                            <div class="mt-3">
                                <span class="inline-block bg-green-100 text-green-800 text-xs px-2 py-1 rounded">ReadWriteLock</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 可重入锁/不可重入锁 -->
            <div class="mb-12">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-sync-alt text-blue-500"></i>
                        </div>
                        <h3 class="text-xl font-semibold">可重入锁 vs 不可重入锁</h3>
                    </div>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="p-4 bg-indigo-50 rounded-lg hover-grow">
                            <h4 class="font-semibold text-indigo-700 mb-2 flex items-center">
                                <i class="fas fa-redo mr-2"></i> 可重入锁
                            </h4>
                            <p class="text-gray-700">可重入锁又名递归锁，指同一个线程在外层方法获取锁时，在进入内层方法会自动获取锁。Java中的ReentrantLock和synchronized都是可重入锁。可重入锁可一定程度避免死锁。</p>
                            <div class="mt-3">
                                <span class="inline-block bg-indigo-100 text-indigo-800 text-xs px-2 py-1 rounded mr-2">ReentrantLock</span>
                                <span class="inline-block bg-indigo-100 text-indigo-800 text-xs px-2 py-1 rounded">synchronized</span>
                            </div>
                        </div>
                        
                        <div class="p-4 bg-red-50 rounded-lg hover-grow">
                            <h4 class="font-semibold text-red-700 mb-2 flex items-center">
                                <i class="fas fa-ban mr-2"></i> 不可重入锁
                            </h4>
                            <p class="text-gray-700">不可重入锁指同一个线程多次获取锁会导致死锁。这种锁设计简单但容易引发问题，现代编程中一般使用可重入锁替代。</p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 mt-6 card">
                    <h4 class="font-semibold mb-3">可重入锁实现示例</h4>
                    <div class="code-block p-4 rounded-lg overflow-x-auto">
                        <pre class="text-gray-300"><code>public class RSpitnLock implements Lock {
    private AtomicReference&lt;Thread&gt; currLock = new AtomicReference&lt;&gt;();
    private int count = 0;

    @Override
    public void lock() {
        Thread current = Thread.currentThread();
        if (current == currLock.get()) {
            count++; // 一个线程多次获取锁，count++
            return;
        }
        while (!currLock.compareAndSet(null, current)) { // 获取锁
        }
    }

    @Override
    public void unlock() {
        Thread current = Thread.currentThread();
        if (current == currLock.get()) {
            if (count != 0) {
                count--;
            } else {
                currLock.compareAndSet(current, null); // 当年线程count=0才能释放锁
            }
        }
    }
}</code></pre>
                    </div>
                </div>
            </div>

            <!-- 公平锁/非公平锁 -->
            <div class="mb-12">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-gavel text-blue-500"></i>
                        </div>
                        <h3 class="text-xl font-semibold">公平锁 vs 非公平锁</h3>
                    </div>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="p-4 bg-teal-50 rounded-lg hover-grow">
                            <h4 class="font-semibold text-teal-700 mb-2 flex items-center">
                                <i class="fas fa-balance-scale-right mr-2"></i> 公平锁
                            </h4>
                            <p class="text-gray-700">公平锁指多个线程按照申请锁的顺序来获取锁。公平锁保证了线程获取锁的顺序性，但会带来额外的性能开销。</p>
                            <div class="mt-3">
                                <span class="inline-block bg-teal-100 text-teal-800 text-xs px-2 py-1 rounded">new ReentrantLock(true)</span>
                            </div>
                        </div>
                        
                        <div class="p-4 bg-pink-50 rounded-lg hover-grow">
                            <h4 class="font-semibold text-pink-700 mb-2 flex items-center">
                                <i class="fas fa-random mr-2"></i> 非公平锁
                            </h4>
                            <p class="text-gray-700">非公平锁指多个线程获取锁的顺序并不是按照申请锁的顺序，有可能后申请的线程比先申请的线程优先获取锁。非公平锁的吞吐量比公平锁大。</p>
                            <div class="mt-3">
                                <span class="inline-block bg-pink-100 text-pink-800 text-xs px-2 py-1 rounded mr-2">new ReentrantLock()</span>
                                <span class="inline-block bg-pink-100 text-pink-800 text-xs px-2 py-1 rounded">synchronized</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 分段锁/自旋锁 -->
            <div class="mb-12">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-puzzle-piece text-blue-500"></i>
                        </div>
                        <h3 class="text-xl font-semibold">分段锁 vs 自旋锁</h3>
                    </div>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <div class="p-4 bg-orange-50 rounded-lg hover-grow">
                            <h4 class="font-semibold text-orange-700 mb-2 flex items-center">
                                <i class="fas fa-cubes mr-2"></i> 分段锁
                            </h4>
                            <p class="text-gray-700">分段锁是一种锁的设计，通过将数据分成多个段，每个段独立加锁，从而提高并发度。ConcurrentHashMap就是通过分段锁实现高效并发操作。</p>
                            <div class="mt-3">
                                <span class="inline-block bg-orange-100 text-orange-800 text-xs px-2 py-1 rounded">ConcurrentHashMap</span>
                            </div>
                        </div>
                        
                        <div class="p-4 bg-cyan-50 rounded-lg hover-grow">
                            <h4 class="font-semibold text-cyan-700 mb-2 flex items-center">
                                <i class="fas fa-spinner mr-2"></i> 自旋锁
                            </h4>
                            <p class="text-gray-700">自旋锁是指尝试获取锁的线程不会立即阻塞，而是采用循环的方式去尝试获取锁，减少线程上下文切换的消耗，但会消耗CPU。</p>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- AQS and CAS Section -->
        <section id="aqs-cas" class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-12 h-1 bg-blue-500 mr-4"></div>
                <h2 class="serif text-3xl font-bold">AQS与CAS原理</h2>
            </div>

            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-sitemap text-blue-500"></i>
                        </div>
                        <h3 class="text-xl font-semibold">AQS (AbstractQueuedSynchronizer)</h3>
                    </div>
                    <p class="text-gray-700 mb-4">AQS是并发容器J.U.C下locks包内的一个类，实现了一个FIFO(先进先出)的队列，底层数据结构是一个双向链表。AQS核心思想是：</p>
                    <ul class="list-disc pl-5 space-y-2 text-gray-700">
                        <li>如果共享资源空闲，则将当前请求资源的线程设置为有效的工作线程</li>
                        <li>如果共享资源被占用，则将线程加入到CLH队列中等待</li>
                        <li>CLH队列是一个虚拟的双向队列，仅存在节点之间的关联关系</li>
                        <li>AQS基于CLH队列，用volatile修饰共享变量state</li>
                    </ul>
                    
                    <div class="mt-6 diagram-container">
                        <div class="mermaid">
                            graph LR
                            A[线程1] -->|获取锁| B[AQS State=1]
                            C[线程2] -->|获取锁失败| D[CLH队列]
                            E[线程3] -->|获取锁失败| D
                            B -->|释放锁| F[唤醒CLH队列头节点]
                        </div>
                    </div>
                </div>
                
                <div class="bg-white rounded-xl shadow-md p-6 card">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                            <i class="fas fa-exchange-alt text-blue-500"></i>
                        </div>
                        <h3 class="text-xl font-semibold">CAS (Compare And Swap)</h3>
                    </div>
                    <p class="text-gray-700 mb-4">CAS是解决多线程并行情况下使用锁造成性能损耗的一种机制。CAS操作包含三个操作数：</p>
                    <ul class="list-disc pl-5 space-y-2 text-gray-700">
                        <li>内存位置 (V)</li>
                        <li>预期原值 (A)</li>
                        <li>新值 (B)</li>
                    </ul>
                    <p class="text-gray-700 mt-4">如果内存位置的值与预期原值相匹配，处理器会自动将该位置值更新为新值。否则，处理器不做任何操作。</p>
                    
                    <div class="mt-6 p-4 bg-yellow-50 rounded-lg">
                        <h4 class="font-semibold text-yellow-700 mb-2">ABA问题</h4>
                        <p class="text-gray-700">线程1读取到内容A，线程2修改A为B后又改回A，线程1的CAS操作仍然会成功，但不知道值已被修改过。</p>
                        <p class="text-gray-700 mt-2"><span class="font-semibold">解决方案：</span>携带版本号或时间戳。</p>
                    </div>
                    
                    <div class="mt-6 diagram-container">
                        <div class="mermaid">
                            sequenceDiagram
                            线程1->>内存: 读取值A(版本1)
                            线程2->>内存: 修改值A→B(版本2)
                            线程2->>内存: 修改值B→A(版本3)
                            线程1->>内存: CAS(A→C) 成功
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Summary Section -->
        <section class="bg-white rounded-xl shadow-md p-8 card">
            <div class="flex items-center mb-6">
                <div class="w-10 h-10 bg-blue-100 rounded-full flex items-center justify-center mr-4">
                    <i class="fas fa-lightbulb text-blue-500"></i>
                </div>
                <h2 class="serif text-2xl font-bold">锁选择指南</h2>
            </div>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="p-4 border border-gray-200 rounded-lg hover:border-blue-300 transition">
                    <h3 class="font-semibold mb-2 flex items-center">
                        <i class="fas fa-book-reader text-blue-500 mr-2"></i> 读多写少场景
                    </h3>
                    <p class="text-gray-700">推荐使用 <span class="font-semibold">读写锁(ReadWriteLock)</span> 或 <span class="font-semibold">乐观锁</span>，提高并发读性能。</p>
                </div>
                
                <div class="p-4 border border-gray-200 rounded-lg hover:border-blue-300 transition">
                    <h3 class="font-semibold mb-2 flex items-center">
                        <i class="fas fa-edit text-blue-500 mr-2"></i> 写多场景
                    </h3>
                    <p class="text-gray-700">推荐使用 <span class="font-semibold">悲观锁(synchronized/ReentrantLock)</span>，保证数据一致性。</p>
                </div>
                
                <div class="p-4 border border-gray-200 rounded-lg hover:border-blue-300 transition">
                    <h3 class="font-semibold mb-2 flex items-center">
                        <i class="fas fa-bolt text-blue-500 mr-2"></i> 高性能需求
                    </h3>
                    <p class="text-gray-700">推荐使用 <span class="font-semibold">CAS</span> 或 <span class="font-semibold">分段锁</span>，减少锁竞争。</p>
                </div>
            </div>
            
            <div class="mt-8 p-6 bg-blue-50 rounded-lg">
                <h3 class="font-semibold text-blue-700 mb-3 flex items-center">
                    <i class="fas fa-exclamation-triangle mr-2"></i> 注意事项
                </h3>
                <ul class="list-disc pl-5 space-y-2 text-gray-700">
                    <li>避免锁的粒度过大，尽量减少锁的持有时间</li>
                    <li>注意死锁问题，避免多个锁的交叉持有</li>
                    <li>高并发场景下，优先考虑无锁设计(CAS)或乐观锁</li>
                    <li>合理使用读写锁可以显著提升读多写少场景的性能</li>
                </ul>
            </div>
        </section>
    </div>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8 px-4 sm:px-6 lg:px-8">
        <div class="max-w-5xl mx-auto">
            <div class="flex flex-col items-center">
                <div class="text-xl font-semibold mb-2">技术小馆</div>
                <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition duration-300">http://www.yuque.com/jtostring</a>
            </div>
            <div class="mt-8 pt-8 border-t border-gray-800 text-center text-sm">
                &copy; 2023 技术小馆. All rights reserved.
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            fontFamily: 'Noto Sans SC, sans-serif'
        });
    </script>
</body>
</html>
```